package android.slc.commonlibrary.util.compat;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;

import androidx.annotation.AttrRes;
import androidx.annotation.IdRes;
import androidx.annotation.StyleableRes;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.Utils;

/**
 * @author android.slc
 * @date 2021/7/13 10:55
 */
public class SlcAttrUtils {
    public static int getDimen(@AttrRes int attrRes) {
        return getColor(attrRes, 0);
    }

    public static int getDimen(Context context, @AttrRes int attrRes) {
        return getDimen(context, attrRes, 0);
    }

    public static int getDimen(@AttrRes int attrRes, int defValue) {
        return getDimen(Utils.getApp(), attrRes, defValue);
    }

    public static int getDimen(Context context, @AttrRes int attrRes, int defValue) {
        @SuppressLint("ResourceType") @StyleableRes()
        int[] ints = {attrRes};
        TypedArray typedArray = context.obtainStyledAttributes(ints);
        int pixelSize = typedArray.getDimensionPixelSize(0, defValue);
        typedArray.recycle();
        return pixelSize;
    }

    public static int getColor(@AttrRes int attrRes) {
        return getColor(attrRes, 0);
    }

    public static int getColor(Context context, @AttrRes int attrRes) {
        return getColor(context, attrRes, 0);
    }

    public static int getColor(@AttrRes int attrRes, int defValue) {
        return getColor(Utils.getApp(), attrRes, defValue);
    }

    public static int getColor(Context context, @AttrRes int attrRes, int defValue) {
        @SuppressLint("ResourceType") @StyleableRes()
        int[] ints = {attrRes};
        TypedArray typedArray = context.obtainStyledAttributes(ints);
        int color = typedArray.getColor(0, defValue);
        typedArray.recycle();
        return color;
    }

    public static Drawable getDrawable(@AttrRes int attrRes) {
        return getDrawable(Utils.getApp(), attrRes);
    }

    public static Drawable getDrawable(Context context, @AttrRes int attrRes) {
        return getDrawable(context, attrRes, null);
    }

    public static Drawable getDrawable(@AttrRes int attrRes, int defValue) {
        return getDrawable(Utils.getApp(), attrRes, ContextCompat.getDrawable(Utils.getApp(), defValue));
    }

    public static Drawable getDrawable(@AttrRes int attrRes, Drawable defValue) {
        return getDrawable(Utils.getApp(), attrRes, defValue);
    }

    public static Drawable getDrawable(Context context, @AttrRes int attrRes, int defValue) {
        return getDrawable(context, attrRes, ContextCompat.getDrawable(context, defValue));
    }

    public static Drawable getDrawable(Context context, @AttrRes int attrRes, Drawable defValue) {
        @SuppressLint("ResourceType") @StyleableRes()
        int[] ints = {attrRes};
        TypedArray typedArray = context.obtainStyledAttributes(ints);
        Drawable drawable = typedArray.getDrawable(0);
        typedArray.recycle();
        return drawable == null ? defValue : drawable;
    }

    public static void getTypedArrayByAttrRes(Context context, @AttrRes int attrRes, OnTypedArrayCallback onTypedArrayCallback) {
        @SuppressLint("ResourceType") @StyleableRes()
        int[] ints = {attrRes};
        TypedArray typedArray = context.obtainStyledAttributes(ints);
        if (onTypedArrayCallback != null) {
            try {
                onTypedArrayCallback.onTypedArray(typedArray);
            } catch (Exception e) {
            }
        }
        typedArray.recycle();
    }

    public static int getResId(@AttrRes int attrRes) {
        return getResId(Utils.getApp(), attrRes);
    }

    public static int getResId(Context context, @AttrRes int attrRes) {
        return getResId(context, attrRes, TypedValue.TYPE_NULL);
    }

    public static int getResId(@AttrRes int attrRes, @IdRes int defResId) {
        return getResId(Utils.getApp(), attrRes, defResId);
    }

    public static int getResId(Context context, @AttrRes int attrRes, @IdRes int defResId) {
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(attrRes, typedValue, true);
        return typedValue.resourceId == TypedValue.TYPE_NULL ? defResId : typedValue.resourceId;
    }

    public interface OnTypedArrayCallback {
        void onTypedArray(TypedArray typedArray);
    }
}
